package com.ty.service;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ty.dao.AccountDao;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class EnumService {


    //ProductStatus
    @SneakyThrows // [{code:1,message:"正常"},{code:2,message:"下架"}]
    public JSONArray select(String enumClassName) {
        //通过反射获取到当前枚举类
        JSONArray array = new JSONArray();
        Class<?> enumClass = Class.forName("com.ty.enums." + enumClassName);
        Object[] enumConstants = enumClass.getEnumConstants();
        for (Object enumConstant : enumConstants){
            Object code = ReflectUtil.getFieldValue(enumConstant, "code");
            Object message = ReflectUtil.getFieldValue(enumConstant, "message");
            JSONObject object = new JSONObject();
            object.putOpt("code",code);
            object.putOpt("message",message);
            array.add(object);
        }
        return  array;

    }
}